/*     */ package com.newcrm.util;
/*     */ 
/*     */ import com.newcrm.bean.CustomerSearchCondition;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerFieldManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ 
/*     */ public class ExportAllCustomerData
/*     */ {
/*  32 */   private Map<Integer, List<Map<String, Object>>> customerFields = new HashMap();
/*     */ 
/*  34 */   private Map<Integer, List<User>> companyUsers = new HashMap();
/*     */ 
/*     */   private String getUserName(int companyId, String userId)
/*     */   {
/*  41 */     List users = (List)this.companyUsers.get(Integer.valueOf(companyId));
/*  42 */     if (users == null) {
/*  43 */       return null;
/*     */     }
/*  45 */     for (Object uO : users) {
                User u = (User)uO;
/*  46 */       if (u.userId.equals(userId)) {
/*  47 */         return u.name;
/*     */       }
/*     */     }
/*  50 */     return null;
/*     */   }
/*     */ 
/*     */   private void searchExtInfo(Connection conn, List<Map<String, Object>> result, int companyId)
/*     */   {
/*  61 */     Map customerIdIndex = new HashMap();
/*  62 */     StringBuffer customerIds = new StringBuffer();
/*     */ 
/*  64 */     for (int i = 0; i < result.size(); i++) {
/*  65 */       Map customer = (Map)result.get(i);
/*     */ 
/*  68 */       if (Util.str2Int(customer.get("Visible")) == 1) {
/*  69 */         customer.put("PermissionsName", "公共数据");
/*     */       }
/*  71 */       else if (customer.get("PermissionsId") == null) {
/*  72 */         customer.put("PermissionsName", "公共数据");
/*     */       } else {
/*  74 */         String currentPermissionsId = 
/*  75 */           (String)customer
/*  75 */           .get("PermissionsId");
/*  76 */         customer.put("PermissionsName", 
/*  77 */           getUserName(companyId, currentPermissionsId));
/*     */       }
/*     */ 
/*  82 */       customerIdIndex.put(Integer.valueOf(Util.str2Int(customer.get("CustomerId"))), Integer.valueOf(i));
/*  83 */       if (customerIds.length() > 0) {
/*  84 */         customerIds.append(",");
/*     */       }
/*     */ 
/*  87 */       customerIds.append(customer.get("CustomerId").toString());
/*     */     }
/*     */ 
/*  91 */     if (customerIds.length() > 0)
/*  92 */       getCustomerExtInfo(conn, customerIds.toString(), result, 
/*  93 */         customerIdIndex);
/*     */   }
/*     */ 
/*     */   private void getCustomerExtInfo(Connection conn, String ids, List<Map<String, Object>> result, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 109 */     CustomerManager manager = new CustomerManager();
/*     */     try
/*     */     {
/* 113 */       List extInfos = manager.getCustomerAllExtInfo(
/* 114 */         conn, ids);
/* 115 */       fillExtInfo("company", result, extInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 117 */       System.out.println("get customers ext info failed: " + e);
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 122 */       List peopleExtInfos = manager.getPeoplesName(
/* 123 */         conn, ids);
/* 124 */       fillPeopleNameInfo(result, peopleExtInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 126 */       System.out.println("get customers ext info failed:" + e);
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 131 */       List peopleExtInfos = manager
/* 132 */         .getPeopleAllExtInfo(conn, ids);
/* 133 */       fillExtInfo("people", result, peopleExtInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 135 */       System.out.println("get customers ext info failed:" + e);
/*     */     }
/*     */   }
/*     */ 
/*     */   private void fillExtInfo(String prefix, List<Map<String, Object>> result, List<Map<String, Object>> extInfos, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 149 */     if ((extInfos == null) || (extInfos.size() == 0)) {
/* 150 */       return;
/*     */     }
/* 152 */     String extPrefix = prefix;
/* 153 */     for (Map extInfo : extInfos)
/*     */     {
/* 155 */       int id = Util.str2Int(extInfo.get("Id"));
/* 156 */       Integer idx = (Integer)customerIdIndex.get(Integer.valueOf(id));
/*     */ 
/* 158 */       if (idx != null)
/*     */       {
/* 162 */         if (idx.intValue() >= 0)
/*     */         {
/* 166 */           if (idx.intValue() <= result.size())
/*     */           {
/* 171 */             Map customer = (Map)result.get(idx.intValue());
/* 172 */             if (customer != null)
/*     */             {
/* 177 */               if (("company".equals(extPrefix)) && 
/* 178 */                 (customer.get("Type") != null)) {
/* 179 */                 int customerType = ((Integer)customer.get("Type")).intValue();
/* 180 */                 if (1 == customerType) {
/* 181 */                   extPrefix = "people";
/*     */                 }
/*     */ 
/*     */               }
/*     */ 
/* 195 */               int type = Util.str2Int(extInfo.get("Type"));
/* 196 */               String value = (String)extInfo.get("Info1");
/*     */ 
/* 200 */               if ((value != null) && (value.trim().length() != 0))
/*     */               {
/* 204 */                 String oldValue = (String)customer.get(extPrefix + "ExtInfo_" + 
/* 205 */                   type);
/* 206 */                 if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 207 */                   value = oldValue + " " + value;
/*     */                 }
/* 209 */                 customer.put(extPrefix + "ExtInfo_" + type, value);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void fillPeopleNameInfo(List<Map<String, Object>> result, List<Map<String, Object>> extInfos, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 224 */     if ((extInfos == null) || (extInfos.size() == 0)) {
/* 225 */       return;
/*     */     }
/* 227 */     for (Map extInfo : extInfos)
/*     */     {
/* 229 */       int id = Util.str2Int(extInfo.get("Id"));
/* 230 */       Integer idx = (Integer)customerIdIndex.get(Integer.valueOf(id));
/*     */ 
/* 232 */       if (idx != null)
/*     */       {
/* 236 */         if (idx.intValue() >= 0)
/*     */         {
/* 240 */           if (idx.intValue() <= result.size())
/*     */           {
/* 245 */             Map customer = (Map)result.get(idx.intValue());
/* 246 */             if (customer != null)
/*     */             {
/* 250 */               String name = (String)extInfo.get("Name");
/* 251 */               String title = (String)extInfo.get("Title");
/* 252 */               if ((name != null) && (name.trim().length() != 0))
/*     */               {
/* 257 */                 String oldValue = (String)customer.get("people");
/* 258 */                 if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 259 */                   name = oldValue + " " + name;
/*     */                 }
/* 261 */                 customer.put("people", name);
/*     */ 
/* 264 */                 if ((title != null) && (title.trim().length() != 0))
/*     */                 {
/* 267 */                   oldValue = (String)customer.get("title");
/* 268 */                   if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 269 */                     title = oldValue + " " + title;
/*     */                   }
/* 271 */                   customer.put("title", title);
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean isShowField(int customerType, int fieldType)
/*     */   {
/* 284 */     if (customerType == -1) {
/* 285 */       return true;
/*     */     }
/*     */ 
/* 288 */     if (fieldType == 0) {
/* 289 */       return true;
/*     */     }
/*     */ 
/* 293 */     if (customerType == 1) {
/* 294 */       return fieldType == 2;
/*     */     }
/*     */ 
/* 297 */     if (customerType == 0) {
/* 298 */       return fieldType == 1;
/*     */     }
/* 300 */     return false;
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 308 */     int companyId = Integer.parseInt(args[0]);
/* 309 */     String filePath = args[1];
/* 310 */     int fileSize = Integer.parseInt(args[2]);
/*     */ 
/* 312 */     ExportAllCustomerData exportData = new ExportAllCustomerData();
/*     */ 
/* 314 */     User user = new User();
/* 315 */     user.userId = "admin";
/* 316 */     user.companyId = companyId;
/* 317 */     user.isAdmin = true;
/*     */ 
/* 319 */     System.out.println("Begin the export");
/*     */ 
/* 333 */     Map parameters = new HashMap();
/* 334 */     parameters.put("Visible", String.valueOf(4));
/* 335 */     parameters.put("PermissionsId", "*");
/* 336 */     parameters.put("CategoryIds", "55,57,58,59,60,61,62,750,0");
/* 337 */     CustomerSearchCondition searchCondition = new CustomerSearchCondition(
/* 338 */       parameters, user, null);
/* 339 */     exportData.ExportDataToFile(filePath + "\\Person", fileSize, 
/* 340 */       exportData, user, searchCondition);
/*     */ 
/* 342 */     System.out.println("Finish the export");
/*     */   }
/*     */ 
/*     */   private void ExportDataToFile(String filePath, int fileSize, ExportAllCustomerData exportData, User user, CustomerSearchCondition searchCondition)
/*     */   {
/* 349 */     List fields = 
/* 350 */       (List)exportData.customerFields
/* 350 */       .get(Integer.valueOf(user.companyId));
/*     */ 
/* 352 */     if (fields == null) {
/* 353 */       CustomerFieldManager customerFiledManager = new CustomerFieldManager();
/*     */       try {
/* 355 */         fields = customerFiledManager.listAll(user.companyId);
/*     */ 
/* 357 */         exportData.customerFields.put(Integer.valueOf(user.companyId), fields);
/*     */       } catch (Exception e) {
/* 359 */         System.out.println("get all customer ext field failed：" + e);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 364 */     List users = (List)exportData.companyUsers.get(Integer.valueOf(user.companyId));
/* 365 */     if ((users == null) || (users.size() == 0)) {
/* 366 */       UserManager userManager = new UserManager();
/*     */       try {
/* 368 */         users = userManager.getCompanyUsers(user.companyId, null);
/* 369 */         exportData.companyUsers.put(Integer.valueOf(user.companyId), users);
/* 370 */         System.out.println("员工信息  size is " + users.size());
/*     */       } catch (Exception e) {
/* 372 */         System.out.println("get company user field failed：" + e);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 377 */     List result = new ArrayList();
/* 378 */     CustomerManager manager = new CustomerManager();
/* 379 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 380 */     if (conn == null) {
/* 381 */       return;
/*     */     }
/* 383 */     int offset = 0;
/*     */     try {
/* 385 */       result = manager.getList(conn, searchCondition, offset, 
/* 386 */         10, user, true);
/*     */ 
/* 388 */       if ((result == null) || (result.size() == 0)) {
/* 389 */         System.out.println("搜索结果是0， 处理下一个导出任务");
/* 390 */         return;
/*     */       }
/*     */ 
/* 394 */       exportData.searchExtInfo(conn, result, user.companyId);
/*     */ 
/* 396 */       System.out.println("Begin the excel creation.");
/*     */ 
/* 398 */       List heads = new ArrayList();
/* 399 */       List headsKey = new ArrayList();
/* 400 */       heads.add("名称");
/* 401 */       headsKey.add("Name");
/* 402 */       heads.add("客户分类");
/* 403 */       headsKey.add("CategoryName");
/* 404 */       heads.add("所属员工");
/* 405 */       headsKey.add("PermissionsName");
/*     */ 
/* 407 */       heads.add("更新时间");
/* 408 */       headsKey.add("UpdateDate");
/*     */ 
/* 412 */       heads.add("联系人");
/* 413 */       headsKey.add("people");
/* 414 */       heads.add("职位");
/* 415 */       headsKey.add("title");
/*     */ 
/* 418 */       heads.add("联系人电话");
/* 419 */       headsKey.add("peopleExtInfo_1");
/*     */ 
/* 422 */       heads.add("联系人邮箱");
/* 423 */       headsKey.add("peopleExtInfo_2");
/*     */ 
/* 426 */       heads.add("联系人QQ");
/* 427 */       headsKey.add("peopleExtInfo_3");
/*     */ 
/* 430 */       for (Object fieldO : fields) {
                    Map field = (Map) fieldO;
/* 431 */         int fieldId = Util.str2Int(field.get("CustomerFieldId"));
/* 432 */         if (fieldId >= 99)
/*     */         {
/* 436 */           int fieldType = Util.str2Int(field.get("FieldType"));
/* 437 */           if (exportData.isShowField(1, fieldType)) {
/* 438 */             heads.add((String)field.get("CustomerFieldName"));
/* 439 */             headsKey.add("peopleExtInfo_" + fieldId);
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 444 */       heads.add("公司电话");
/* 445 */       headsKey.add("companyExtInfo_1");
/*     */ 
/* 448 */       heads.add("公司邮箱");
/* 449 */       headsKey.add("companyExtInfo_2");
/*     */ 
/* 452 */       heads.add("公司QQ");
/* 453 */       headsKey.add("companyExtInfo_3");
/*     */ 
/* 456 */       heads.add("网站");
/* 457 */       headsKey.add("companyExtInfo_4");
/*     */ 
/* 460 */       heads.add("地址");
/* 461 */       headsKey.add("companyExtInfo_6");
/*     */ 
/* 464 */       heads.add("简介");
/* 465 */       headsKey.add("Note");
/*     */ 
/* 468 */       for (Object fieldO : fields) {
            Map field = (Map) fieldO;
/* 469 */         int fieldId = Util.str2Int(field.get("CustomerFieldId"));
/* 470 */         if (fieldId >= 99)
/*     */         {
/* 474 */           int fieldType = Util.str2Int(field.get("FieldType"));
/* 475 */           if (exportData.isShowField(0, fieldType)) {
/* 476 */             heads.add((String)field.get("CustomerFieldName"));
/* 477 */             headsKey.add("companyExtInfo_" + fieldId);
/*     */           }
/*     */         }
/*     */       }
/* 481 */       int fileIndex = 1;
/* 482 */       String fileName = Util.date2Str2(new Date());
/* 483 */       filePath = filePath + fileName + "_" + fileIndex + ".xls";
/* 484 */       System.out.println("filePath is " + filePath);
/*     */ 
/* 486 */       FileOutputStream out = new FileOutputStream(filePath);
/* 487 */       ExcelUtil.export("客户信息", (String[])heads.toArray(new String[0]), 
/* 488 */         (String[])headsKey.toArray(new String[0]), result, null, out);
/*     */ 
/* 490 */       int index = 0;
/* 491 */       int rowOffset = 0;
/*     */       do {
/* 493 */         filePath = filePath.split("_")[0] + "_" + fileIndex + ".xls";
/*     */ 
/* 495 */         offset += 10;
/* 496 */         rowOffset += 10;
/* 497 */         result = manager.getList(conn, searchCondition, offset, 
/* 498 */           10, user, true);
/*     */ 
/* 500 */         exportData.searchExtInfo(conn, result, user.companyId);
/* 501 */         System.out.println("当前导出条数 " + offset);
/*     */ 
/* 504 */         if (index >= fileSize) {
/* 505 */           fileIndex++;
/* 506 */           index = 0;
/* 507 */           rowOffset = 0;
/* 508 */           filePath = filePath.split("_")[0] + "_" + fileIndex + 
/* 509 */             ".xls";
/* 510 */           out = new FileOutputStream(filePath);
/* 511 */           ExcelUtil.export("客户信息", (String[])heads.toArray(new String[0]), 
/* 512 */             (String[])headsKey.toArray(new String[0]), result, null, out);
/* 513 */           System.out.println("当前导出文件条数 " + offset);
/* 514 */           System.out.println("创建新的文件 " + filePath);
/*     */         }
/*     */ 
/* 518 */         ExcelUtil.addData(filePath, (String[])headsKey.toArray(new String[0]), 
/* 519 */           result, null, rowOffset);
/* 520 */         index++;
/*     */ 
/* 492 */         if (result == null) break;  } while (result.size() >= 10);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 524 */       e.printStackTrace();
/* 525 */       System.out.println("export customers info failed: " + e);
/*     */       try
/*     */       {
/* 528 */         DbUtils.close(conn);
/*     */       } catch (SQLException se) {
/* 530 */         System.out.println("close conn failed:" + se);
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 528 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 530 */         System.out.println("close conn failed:" + e);
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.util.ExportAllCustomerData
 * JD-Core Version:    0.6.1
 */